package cn.kgc.day05.servlet;

import cn.kgc.day05.entity.User;
import cn.kgc.day05.http.ResponseResult;
import cn.kgc.day05.service.UserService;
import cn.kgc.day05.service.impl.UserServiceImpl;
import cn.kgc.day05.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 课工场
 * @date 2024/10/16
 * @description
 */
@WebServlet("/user")
public class UserServlet extends BaseServlet {


    private UserService userService = new UserServiceImpl();

    // 登录  login
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        ResponseResult responseResult = userService.login(username, password);
        if (responseResult.getCode() == 0) {
            HttpSession session = req.getSession();
            session.setAttribute("username", username);
        }

        ResponseUtil.writeJson(responseResult, resp);
    }

    // 注册用户
    protected void registerUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        ResponseResult responseResult = userService.registerUser(username, password);
        ResponseUtil.writeJson(responseResult, resp);

    }


    // 根据用户名查询用户信息  用户名验重
    protected void selectByUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        ResponseResult responseResult = userService.selectByUsername(username);
        ResponseUtil.writeJson(responseResult, resp);

    }


    // personalInfo  个人信息查询
    protected void personalInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();
        String username = (String) session.getAttribute("username");
        System.out.println("username = " + username);
        ResponseResult responseResult = userService.personalInfo(username);
        ResponseUtil.writeJson(responseResult, resp);

    }

    // editPersonalInfo 编辑个人信息
    protected void editPersonalInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer id = Integer.valueOf(req.getParameter("id"));
        String tel = req.getParameter("tel");
        String sex = req.getParameter("sex");
        Integer age = Integer.valueOf(req.getParameter("age"));

        User user = new User();
        user.setId(id);
        user.setAge(age);
        user.setTel(tel);
        user.setSex(sex);

        ResponseResult responseResult = userService.editPersonalInfo(user);

        ResponseUtil.writeJson(responseResult, resp);

    }

    // editPersonalPwd  修改个人密码
    protected void editPersonalPwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String oldPwd = req.getParameter("oldPwd");
        String newPwd = req.getParameter("newPwd");
        HttpSession session = req.getSession();
        String username = (String) session.getAttribute("username");


        ResponseResult responseResult = userService.editPersonalPwd(oldPwd, newPwd, username);

        ResponseUtil.writeJson(responseResult, resp);

    }

    // logout  安全退出
    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();
        // 销毁session
        session.invalidate();

        // 删除指定的key
        // session.removeAttribute();

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("安全退出");

        ResponseUtil.writeJson(responseResult,resp);
    }


}
